<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script type="text/javascript">
        function Teacher() {
            // 在没有实例化之前是不存在this的
            this.name = "zhangsan";
            this.sex = "男";
            this.smoke = function () {

            }
        }
        let teacher1 = new Teacher();
        let teacher2 = new Teacher();
        teacher1.name = "赵四";
        teacher2.name = "王五";
        // 通过构造函数构造出来的对象是完全互不干扰的，改变其中的一个对象的属性或方法是不会影响其他对象的属性和方法的
        console.log(teacher1, teacher2);

        function Test(...arg) {
            arg.forEach(item => {
                    if (typeof (item) !== 'number') {
                        alert("参数错误");
                    }
                }) 
                +function () {
                    let num = 0;
                    for (let i = 0; i < arg.length; i++) {
                        num += arg[i];
                    }
                    console.log('相加：' + num);
                }() 
                +function () {
                    let num = 1;
                    for (let i = 0; i < arg.length; i++) {
                        num *= arg[i];
                    }
                    console.log('相乘：' + num);
                }()
        }
        let t1 = new Test(1, 2, 3, 4, 5, 8);

        function Car(opt) {
            this.brand = opt.brand;
            this.color = opt.color;
            this.displacement = opt.displacement;
            
            // 构造函数中 return原始值无效 只能return引用类型值 对象 函数 数组等
            // return 'a';
            // return 1;
            // return undefined;

        }
        function Person(opt){
            this.name = opt.name;
            this.age = opt.age;
            this.income = opt.income;
            this.selectCar = function () {
                let myCar = new Car(opt.carOpt);
                console.log(`${this.name}买了一辆${myCar.displacement}的${myCar.color}${myCar.brand}`);
            }
        }
        
        let user = new Person({
            name:'小强',
            age:28,
            income:'30K',
            carOpt:{ 
                brand:'马自达',
                color:'红色',
                displacement:'2.0'
            }
        });
        user.selectCar();

        // 原始值没有属性

        // 字符串有length属性是因为通过包装类new String产生的
        let str = 'aaa';
        // console.log(str.length);
        console.log(new String(str).length);

    
    </script>
</body>

</html>